What is resolve-options?
The resolve-options npm package is designed to help developers manage and resolve configuration options for their applications or libraries. It provides a structured way to define defaults, validate types, and handle option resolution logic in a centralized manner.
What are resolve-options's main functionalities?
Defining and resolving options
This feature allows developers to define a schema for options including types and default values, and then resolve them against user-provided values. The example shows how to define a simple configuration and resolve it.
const resolveOptions = require('resolve-options');
const config = {
option: {
type: 'string',
default: 'default value'
}
};
const resolved = resolveOptions(config, { option: 'custom value' });
console.log(resolved.option); // Outputs: 'custom value'
Type validation
This feature ensures that the options provided by the user match the expected type defined in the configuration schema. The example demonstrates type validation where a type mismatch throws an error.
const resolveOptions = require('resolve-options');
const config = {
option: {
type: 'number',
default: 10
}
};
try {
const resolved = resolveOptions(config, { option: 'not a number' });
} catch (error) {
console.error(error); // Outputs: Error indicating the type mismatch
}
Other packages similar to resolve-options
yargs
Yargs helps in building interactive command line tools, by parsing arguments and generating an elegant user interface. It's more focused on CLI arguments parsing compared to resolve-options which is more generic in handling any kind of options.
config
The config package is used for managing configuration settings across different deployment environments. Unlike resolve-options, which is about resolving options dynamically, config is more about static configuration management.
resolve-options
Resolve an options object based on configuration.
Usage
var createResolver = require('resolve-options');
var config = {
cwd: {
type: 'string',
default: process.cwd,
},
sourcemaps: {
type: 'boolean',
default: false,
},
since: {
type: ['date', 'number'],
},
read: {
type: 'boolean',
},
};
var options = {
sourcemaps: true,
since: Date.now(),
read: function (file) {
return file.extname !== '.mp4';
},
};
var resolver = createResolver(config, options);
var cwd = resolver.resolve('cwd', file);
var sourcemaps = resolver.resolve('sourcemaps', file);
var read = resolver.resolve('read', file);
API
createResolver([config,] [options])
Takes a config
object that describes the options to accept/resolve and an options
object (usually passed by a user) to resolve against the config
. Returns a resolver
that contains a resolve
method for realtime resolution of options.
The config
object takes the following structure:
config {
[optionKey] {
type // string, array or function
default // any value or function
}
}
Each option is represented by its optionKey
in the config
object. It must be an object with a type
property.
The type
property must be a string, array or function which will be passed to the value-or-function
module (functions will be bound to the resolver to allow for dependent options).
A default
property may also be specified as a fallback if the option isn't available or is invalid. The default
value can be any value or a function (functions will be bound to the resolver to allow for dependent defaults). Note: default
values are not type-validated by the value-or-function
module.
resolver.resolve(optionKey, [...arguments])
Takes an optionKey
string and any number of arguments
to apply if an option is a function. Returns the resolved value for the optionKey
.
resolver.resolveConstant(optionKey)
Like resolve
, but only returns a value if the option is constant (not a function).
License
MIT